package com.example.demo.sao.impl;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.example.demo.sao.ICacheSao;
import com.example.demo.service.IDataBaseCacheRepositoryService;
import com.example.demo.service.IRedisCacheRepositoryService;
import com.example.demo.service.impl.DataBaseCacheRepositoryServiceImpl;

import lombok.extern.log4j.Log4j2;

@Log4j2 
@Component
public class CacheSaoImpl implements ICacheSao,InitializingBean {

	@Value("${demo-settings.ApplicationCacheScheme}")
	private String applicationCacheScheme;

	@Autowired
	private IRedisCacheRepositoryService redisCacheRepositoryService;
	
	@Autowired
	private IDataBaseCacheRepositoryService dataBaseCacheRepositoryService;	

	//构造器里面该对象还没有实例化，所以这个注入的对象是为null
    @Override
    public void afterPropertiesSet() throws Exception {    	
    	 log.info("applicationCacheScheme:"+applicationCacheScheme);	
    }
    
    public boolean Exists(String key) {
    	if("database-cache".equals(applicationCacheScheme))
			return dataBaseCacheRepositoryService.Exists(key);	
		else
			return redisCacheRepositoryService.Exists(key);
    }
	
	public Object Get(String key) {		
		if("database-cache".equals(applicationCacheScheme))
			return dataBaseCacheRepositoryService.Get(key);	
		else
			return redisCacheRepositoryService.Get(key);
	}	
	 
	public void Set(String key,Object value) {
		if("database-cache".equals(applicationCacheScheme))
			 dataBaseCacheRepositoryService.Set(key, value);
		else
			 redisCacheRepositoryService.Set(key, value);
	}
	
	public void Delete(String key) {
		if("database-cache".equals(applicationCacheScheme))
			 dataBaseCacheRepositoryService.Delete(key);
		else
			 redisCacheRepositoryService.Delete(key);
	}
	 
	public void Clear() {
		if("database-cache".equals(applicationCacheScheme))
			 dataBaseCacheRepositoryService.Clear();
		else
			 redisCacheRepositoryService.Clear();
	}	
	
}
